package com.example.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.example.mapper.master.OrderMapper;
import com.example.model.Order;
import com.example.service.OrderService;
import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author long
 * @date 2024/12/11 14:22
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

//    @Resource
//    private RocketMQTemplate rocketMQTemplate;

    @Override
    public String createOrder(Order order) {
        Snowflake snowflake = new Snowflake();
        String id = snowflake.nextIdStr();

        // 新增订单
        order.setId(id);
        order.setPay(0);  // 已经支付
        order.setValid(1); // 有效订单
        Date curDate = new Date();
        order.setDeadline(new Date(curDate.getTime()+ 10 * 1000L)); // 当前时间+10s
        orderMapper.insert(order);

        log.info("当前时间：{}",curDate);

        // 发送延时消息
//        rocketMQTemplate.syncSend("order-create",
//                MessageBuilder.withPayload(id).build(),3000, 3); // 10s

        // 延迟等级 https://rocketmq.apache.org/zh/docs/4.x/producer/04message3/

        return id;
    }

}
